package com.jbm.core.common;

import com.jbm.core.iface.IAcross;
import com.jbm.core.pb.PBIAcross;

/**
 * Created by root on 16-10-14.
 */
public class PBRandR implements RandR
{
	public PBRandR ()
	{
	}

	public PBRandR ( PBIAcross.IAcross iacross )
	{
		this();

		m_iacross = iacross;
	}

	@Override
	public int islastresult ( PBIAcross.IAcross iacross )
	{
		return 1;//1:close 0:keep 2:delay&close
	}

	@Override
	public PBIAcross.IAcross BuildResponse ( IAcross sresp )
	{
		PBIAcross.IAcross.Builder builder = new TransClass().I2_0toPB2_0Builder("Server:",sresp);

		if ( (m_iacross.getRandr()&RANDR_TYPE_TASK)==RANDR_TYPE_TASK)
			builder.setRandr(RandR.RANDR_TYPE_RESPONSE|RANDR_TYPE_TASK);
		else
			builder.setRandr(RandR.RANDR_TYPE_RESPONSE);


//		if ( m_iacross.hasCaKey())
//			builder.setCaKey(m_iacross.getCaKey());
//		else if ( sresp.m_ca_key != null )
//			builder.setCaKey(sresp.m_ca_key);
		if ( m_iacross.hasQuery())
			builder.setQuery(m_iacross.getQuery());
		if ( m_iacross.hasFromid())
			builder.setFromid(m_iacross.getFromid());
		if ( m_iacross.hasToid())
			builder.setToid(m_iacross.getToid());
		if ( m_iacross.hasFromtag())
			builder.setFromtag(m_iacross.getFromtag());
		if ( m_iacross.hasTotag())
			builder.setTotag(m_iacross.getTotag());

		if ( !builder.hasCode() )
		{
			builder.setCode(200);
			if (!builder.hasInfo())
				builder.setInfo("OK");
		}else if ( builder.getCode() == 200 )
		{
			if (!builder.hasInfo())
				builder.setInfo("OK");
		}else
			if (!builder.hasInfo())
				builder.setInfo("Unknown error");

		builder.setTimestamp(System.currentTimeMillis());

		return builder.build();
	}

	@Override
	public PBIAcross.IAcross BuildTimeoutResponse ()
	{
		PBIAcross.IAcross.Builder builder = PBIAcross.IAcross.newBuilder();

		builder.setCode(408);
		builder.setInfo("request timeout");

		return builder.build();
	}

	@Override
	public PBIAcross.IAcross BuildRequest ( String query,
											String classname,
											String method,
											int fromid,
											int toid,
											String fromtag,
											String totag,
											int index,
											boolean excode,
											boolean intask,
											IAcross content )
	{
		PBIAcross.IAcross.Builder builder = null;

		if ( content != null )
		{
			content.m_query = query;
			content.m_fromid = fromid;
			content.m_toid = toid;
			content.m_fromtag = fromtag;
			content.m_totag = totag;
			content.m_qindex = index;
			content.m_excode = 0;

			if ( excode )
				content.m_excode |= IAcross.excode_doexcode;
			if ( intask )
				content.m_excode |= IAcross.excode_intask;

			builder = new TransClass().I2_0toPB2_0Builder("Client:", content);
		}else
		{
			builder = PBIAcross.IAcross.newBuilder();

			if ( query != null )
				builder.setQuery(query);
			if ( fromid != 0 )
				builder.setFromid(fromid);
			if ( toid != 0 )
				builder.setToid(toid);
			if ( fromtag != null )
				builder.setFromtag(fromtag);
			if ( totag != null )
				builder.setTotag(totag);
			if ( index >= 0 )
				builder.setQindex(index);

			builder.setExcode(0);
			if ( excode )
				builder.setExcode(builder.getExcode()|IAcross.excode_doexcode);
			if ( intask )
				builder.setExcode(builder.getExcode()|IAcross.excode_intask);
		}

		builder.setRandr(RandR.RANDR_TYPE_REQUEST);

		builder.setClass_(classname);
		builder.setMethod(method);

		return builder.build();
	}

	public byte[] toByteArray()
	{
		if ( m_iacross != null )
			return m_iacross.toByteArray();
		else
			return null;
	}

	public Object GetCore()
	{
		return m_iacross;
	}

	public PBIAcross.IAcross m_iacross;
}
